#ifndef _WX_HTMPRINT_H_
#define _WX_HTMPRINT_H_

#include "wx/defs.h"
#include "wx/html/htmlcell.h"
#include "wx/html/winpars.h"
#include "wx/html/htmlfilt.h"
#include "wx/print.h"
#include "wx/printdlg.h"
#include <limits.h>

class wxHtmlDCRenderer : public wxObject {
  public:
    wxHtmlDCRenderer();
    virtual ~wxHtmlDCRenderer();
    void SetDC( wxDC *dc, double pixel_scale = 1.0 );
    void SetSize( int width, int height );
    void SetHtmlText( const wxString& html, const wxString& basepath = wxEmptyString, bool isdir = true );
    void SetFonts( const wxString& normal_face, const wxString& fixed_face, const int *sizes = NULL );
    void SetStandardFonts( int size = -1,
                           const wxString& normal_face = wxEmptyString,
                           const wxString& fixed_face = wxEmptyString );
    int Render( int x, int y, wxArrayInt& known_pagebreaks, int from = 0,
                int dont_render = FALSE, int to = INT_MAX );
    int GetTotalHeight();
  private:
    wxDC *m_DC;
    wxHtmlWinParser *m_Parser;
    wxFileSystem *m_FS;
    wxHtmlContainerCell *m_Cells;
    int m_MaxWidth, m_Width, m_Height;
    DECLARE_NO_COPY_CLASS( wxHtmlDCRenderer )
};

enum {
  wxPAGE_ODD,
  wxPAGE_EVEN,
  wxPAGE_ALL
};

class wxHtmlPrintout : public wxPrintout {
  public:
    wxHtmlPrintout( const wxString& title = wxT( "Printout" ) );
    virtual ~wxHtmlPrintout();

    void SetHtmlText( const wxString& html, const wxString &basepath = wxEmptyString, bool isdir = true );
    void SetHtmlFile( const wxString &htmlfile );
    void SetHeader( const wxString& header, int pg = wxPAGE_ALL );
    void SetFooter( const wxString& footer, int pg = wxPAGE_ALL );
    void SetFonts( const wxString& normal_face, const wxString& fixed_face, const int *sizes = NULL );
    void SetStandardFonts( int size = -1,
                           const wxString& normal_face = wxEmptyString,
                           const wxString& fixed_face = wxEmptyString );

    void SetMargins( float top = 25.2, float bottom = 25.2, float left = 25.2, float right = 25.2,
                     float spaces = 5 );
    bool OnPrintPage( int page );
    bool HasPage( int page );
    void GetPageInfo( int *minPage, int *maxPage, int *selPageFrom, int *selPageTo );
    bool OnBeginDocument( int startPage, int endPage );
    void OnPreparePrinting();
    static void AddFilter( wxHtmlFilter *filter );
    static void CleanUpStatics();
  private:
    void RenderPage( wxDC *dc, int page );
    wxString TranslateHeader( const wxString& instr, int page );
    void CountPages();
  private:
    int m_NumPages;
    wxArrayInt m_PageBreaks;
    wxString m_Document, m_BasePath;
    bool m_BasePathIsDir;
    wxString m_Headers[2], m_Footers[2];
    int m_HeaderHeight, m_FooterHeight;
    wxHtmlDCRenderer *m_Renderer, *m_RendererHdr;
    float m_MarginTop, m_MarginBottom, m_MarginLeft, m_MarginRight, m_MarginSpace;
    static wxList m_Filters;
    DECLARE_NO_COPY_CLASS( wxHtmlPrintout )
};

class wxHtmlEasyPrinting : public wxObject {
  public:
    wxHtmlEasyPrinting( const wxString& name = wxT( "Printing" ), wxWindow *parentWindow = NULL );
    virtual ~wxHtmlEasyPrinting();
    bool PreviewFile( const wxString &htmlfile );
    bool PreviewText( const wxString &htmltext, const wxString& basepath = wxEmptyString );
    bool PrintFile( const wxString &htmlfile );
    bool PrintText( const wxString &htmltext, const wxString& basepath = wxEmptyString );
    void PageSetup();
    void SetHeader( const wxString& header, int pg = wxPAGE_ALL );
    void SetFooter( const wxString& footer, int pg = wxPAGE_ALL );
    void SetFonts( const wxString& normal_face, const wxString& fixed_face, const int *sizes = 0 );
    void SetStandardFonts( int size = -1,const wxString& normal_face = wxEmptyString,
                           const wxString& fixed_face = wxEmptyString );
    wxPrintData *GetPrintData();
    wxPageSetupDialogData *GetPageSetupData() {return m_PageSetupData;}
    wxWindow* GetParentWindow() const { return m_ParentWindow; }
    void SetParentWindow( wxWindow* window ) { m_ParentWindow = window; }
    const wxString& GetName() const { return m_Name; }
    void SetName( const wxString& name ) { m_Name = name; }
  protected:
    virtual wxHtmlPrintout *CreatePrintout();
    virtual bool DoPreview( wxHtmlPrintout *printout1, wxHtmlPrintout *printout2 );
    virtual bool DoPrint( wxHtmlPrintout *printout );
  private:
    wxPrintData *m_PrintData;
    wxPageSetupDialogData *m_PageSetupData;
    wxString m_Name;
    int m_FontsSizesArr[7];
    int *m_FontsSizes;
    wxString m_FontFaceFixed, m_FontFaceNormal;
    enum FontMode {
      FontMode_Explicit,
      FontMode_Standard
    };
    FontMode m_fontMode;
    wxString m_Headers[2], m_Footers[2];
    wxWindow *m_ParentWindow;
    DECLARE_NO_COPY_CLASS( wxHtmlEasyPrinting )
};

#endif
